﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
public partial class AdminPortal_Controls_Media_CategoryFile : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    TblMediaCategoryManager _TblMediaCategoryManager = new TblMediaCategoryManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TblMediaCategoryEntity _TblMediaCategoryEntity = new TblMediaCategoryEntity();
            _TblMediaCategoryEntity.SortExpression = TblDanhMucSanPhamFields.ThuTu.Name;
            _TblMediaCategoryEntity.SortDirection = "ASC";            
            LoadDataToGrid(_TblMediaCategoryEntity);
        }
    }

    private void LoadDataToGrid(TblMediaCategoryEntity _TblMediaCategoryEntity)
    {
        _TblMediaCategoryEntity.Type = "category";
        _TblMediaCategoryEntity.LangId = LangID;
        DataTable dt = _TblMediaCategoryManager.be_SelectToDataList(_TblMediaCategoryEntity);
        if (dt.Rows.Count > 0)
        {
            lblTotalRecord.Text = dt.Rows.Count.ToString();
            dgvListCategoryNews.DataSource = dt;
            dgvListCategoryNews.DataBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/FlieCategoryAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvListCategoryNews.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            if (chkID.Checked)
            {
                try
                {
                    _TblMediaCategoryManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
            LoadDataToGrid(new TblMediaCategoryEntity());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblMediaCategoryEntity _TblMediaCategoryEntity = new TblMediaCategoryEntity();
        _TblMediaCategoryEntity.SortExpression = TblDanhMucSanPhamFields.ThuTu.Name;
        _TblMediaCategoryEntity.SortDirection = "ASC";
        _TblMediaCategoryEntity.Condition = txtKeySearch.Text;
        LoadDataToGrid(_TblMediaCategoryEntity);

    }
    protected void dgvListCategoryNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkCategoryName = (HyperLink)e.Row.FindControl("lnkCategoryName");

        Image imgUpdate = (Image)e.Row.FindControl("imgUpdate");

        Image imgIcon = (Image)e.Row.FindControl("imgIcon");

        if (r != null && lnkCategoryName != null && imgUpdate != null)
        {
            lnkCategoryName.Text = r[TblDanhMucSanPhamFields.TenDanhMuc.Name].ToString();
            lnkCategoryName.NavigateUrl = "~/AdminPortal/Pages/FlieCategoryEdit.aspx?id=" + r[TblDanhMucSanPhamFields.Id.Name].ToString();

            if (r[TblDanhMucSanPhamFields.TrangThai.Name].ToString().Equals("True"))
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_yes.png";
            else
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_no.png";
            imgUpdate.Attributes.Add("onclick", "javascript:changeStatusFlieCategory('" + imgUpdate.ClientID + "','" + r[TblDanhMucSanPhamFields.Id.Name].ToString() + "')");

            if (!string.IsNullOrEmpty(r[TblDanhMucSanPhamFields.Icon.Name].ToString()))
                imgIcon.ImageUrl = r[TblDanhMucSanPhamFields.Icon.Name].ToString();
            else
                imgIcon.Visible = false;
        }
    }
}